package com.lzx.applib.base;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.lzx.applib.R;
import com.lzx.applib.utils.LogUtil;

import static android.R.attr.screenOrientation;

/**
 * Created by Lzx on 16/11/08 0008.
 */

public class BaseActivity extends UmengActivity {

    private static final String TAG = "BaseActivity";

    public Context context;

    @Override
    public void setTheme(@StyleRes int resid) {
        super.setTheme(resid);
        TypedArray attr = getTheme().obtainStyledAttributes(new int[]{screenOrientation});
        int orientation = attr.getInt(0, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        LogUtil.d(TAG, "set screenOrientation by theme:" + orientation);
        attr.recycle();
        int oldOrientation = getRequestedOrientation();
        if (oldOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
            setRequestedOrientation(orientation);
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
    }

    public <T> T find(int id) {
        return (T) findViewById(id);
    }
    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        slideRightIn();
    }

    public void finishNotAnim() {
        super.finish();
    }

    @Override
    public void finish() {
        super.finish();
        slideRightOut();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(),0);
        }
    }

    public void slideRightIn() {
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    public void slideRightOut() {
        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
    }

    public void slideBottomIn() {
        overridePendingTransition(R.anim.slide_bottom_in, 0);
    }

    public void slideBottomOut() {
        overridePendingTransition(0, R.anim.slide_bottom_out);
    }

    public void slideTopIn() {
        overridePendingTransition(R.anim.slide_top_in, 0);
    }

    public void slideTopOut() {
        overridePendingTransition(0, R.anim.slide_top_out);
    }

    public void alphaIn() {
        overridePendingTransition(R.anim.alpha_in, 0);
    }

    public void alphaOut() {
        overridePendingTransition(0, R.anim.alpha_out);
    }

    public void fullScreen() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    public void noFullScreen() {
        WindowManager.LayoutParams attrs = getWindow().getAttributes();
        attrs.flags = attrs.flags & ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setAttributes(attrs);
    }

    public void noTitle() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
